package Bidostar_Page;

import Bidostar_BaseData.Page;
import Bidostar_Data.Baseinfo;
import Bidostar_Data.Bidostar_GetCode;
import org.openqa.selenium.WebDriver;

public class ChangePwPage extends Page
{
    public ChangePwPage(WebDriver driver)
    {
        super(driver);
    }

    public void clickChangePW_Btn()
    {
        getElement("ChangePW_Btn").click();
    }

    public void Inputold_PW(String old_PW)
    {
        getElement("old_PW").clear();
        getElement("old_PW").sendKeys(old_PW);
    }

    public void Inputnew_PW(String new_PW)
    {
        getElement("new_PW").clear();
        getElement("new_PW").sendKeys(new_PW);
    }

    public void InputSure_Pw(String Sure_NewPw)
    {
        getElement("Sure_NewPw").clear();
        getElement("Sure_NewPw").sendKeys(Sure_NewPw);
    }

    public void clickSave_Btn()
    {
        getElement("Save_Btn").click();
    }

    public boolean ChangePw() throws Exception
    {
        try
        {
            clickChangePW_Btn();
            if (driver.getCurrentUrl().contains("ci.bidostar.com"))
            {
                Inputold_PW(Bidostar_GetCode.Ci_Password());
                if (Bidostar_GetCode.Ci_Password().length() == 0)
                {
                    clickSave_Btn();
                    logger.warn(getElement("error_Message").getText());
                } else
                {
                    logger.info("原密码为：" + Bidostar_GetCode.Ci_Password());
                }
            } else if (driver.getCurrentUrl().contains("ta.bidostar.com"))
            {
                Inputold_PW(Bidostar_GetCode.Ta_Password());
                if (Bidostar_GetCode.Ta_Password().length() == 0)
                {
                    clickSave_Btn();
                    logger.warn(getElement("error_Message").getText());
                } else
                {
                    logger.info("原密码为：" + Bidostar_GetCode.Ta_Password());
                }
            } else if (driver.getCurrentUrl().contains("dc.bidostar.com"))
            {
                Inputold_PW(Bidostar_GetCode.Dc_Password());
                if (Bidostar_GetCode.Dc_Password().length() == 0)
                {
                    clickSave_Btn();
                    logger.warn(getElement("error_Message").getText());
                } else
                {
                    logger.info("原密码为：" + Bidostar_GetCode.Dc_Password());
                }
            }
            Inputnew_PW(Baseinfo.New_Pw);
            if (Baseinfo.New_Pw.length() == 0)
            {
                clickSave_Btn();
                logger.warn(getElement("error_Message").getText());
            } else if (Baseinfo.New_Pw.length() < 6)
            {
                clickSave_Btn();
                logger.warn(getElement("error_Message").getText());
            } else if (Baseinfo.New_Pw.length() >= 6 && Baseinfo.New_Pw.length() <= 16)
            {
                if (!Baseinfo.New_Pw.matches("^.*[a-zA-Z]+.*$") || !Baseinfo.New_Pw.matches("^.*[0-9]+.*$"))
                {
                    clickSave_Btn();
                    logger.warn(getElement("error_Message").getText());
                } else if (Baseinfo.New_Pw.matches("^.*[a-zA-Z]+.*$") && Baseinfo.New_Pw.matches("^.*[0-9]+.*$"))
                {
                    logger.info("新密码为：" + Baseinfo.New_Pw);
                    InputSure_Pw(Baseinfo.Sure_Pw);
                    if (Baseinfo.Sure_Pw.length() == 0)
                    {
                        clickSave_Btn();
                        logger.warn(getElement("error_Message").getText());
                    } else if (!Baseinfo.New_Pw.equals(Baseinfo.Sure_Pw))
                    {
                        clickSave_Btn();
                        logger.warn(getElement("error_Message").getText());
                    } else if (Baseinfo.New_Pw == Baseinfo.Sure_Pw)
                    {
                        logger.info("确认密码为：" + Baseinfo.Sure_Pw);
                        clickSave_Btn();
                        return true;
                    }
                }
            } else
            {
                clickSave_Btn();
                logger.warn(getElement("error_Message").getText());
            }
        } catch (Exception e)
        {
            logger.warn("失败");
        }
        return false;
    }
}
